package com.kws.merchant.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kws.merchant.domain.AttentionEntity;
import com.kws.merchant.domain.vo.AttentionVo;
import com.kws.merchant.domain.vo.GoodsCollectVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author : zhangbenting
 * @date : 2024/11/2 9:45
 * @Version: 1.0
 */
public interface AttentionMapper extends BaseMapper<AttentionEntity> {

    @Select("<script>" +
            "SELECT " +
            " a.attention_type_id, " +
            " a.type, " +
            " a.id, " +
            " b.company_name, " +
            " b.logo  " +
            " FROM " +
            " tb_attention_manage AS a " +
            " LEFT JOIN tb_merchant AS b ON a.attention_type_id = b.id  " +
            " WHERE " +
            " a.user_id = ${userId}  " +
            " AND a.type = '2' " +
            " <if test='shopName!=null and shopName!=\"\"'>  AND b.company_name like '%${shopName}%'  </if>  " +
            " limit #{page},#{size}" +
            "</script>")
    List<AttentionVo> getGoodsAttentionList(@Param("userId") Long userId,
                                            @Param("shopName") String shopName,
                                            @Param("page") Integer page,
                                            @Param("size") Integer size);

    @Select("<script>" +
            "SELECT " +
            " count(a.attention_type_id) as `count` " +
            " FROM " +
            " tb_attention_manage AS a " +
            " LEFT JOIN tb_merchant AS b ON a.attention_type_id = b.id  " +
            " WHERE " +
            " a.user_id = ${userId}  " +
            " AND a.type = '2' " +
            " <if test='shopName!=null and shopName!=\"\"'>  AND b.company_name like '%${shopName}%'  </if>  " +
            "</script>")
    Long getGoodsAttentionCount(@Param("userId") Long userId,
                                @Param("shopName") String shopName);

    @Select(" SELECT " +
            "  a.attention_type_id AS goods_id, " +
            "  a.id, " +
            "  a.merchant_id, " +
            "  b.`name` AS goods_name, " +
            "  b.price, " +
            "  b.img  " +
            " FROM " +
            "  tb_attention_manage AS a " +
            "  LEFT JOIN tb_serve AS b ON a.attention_type_id = b.id  " +
            " WHERE " +
            " a.type = 1 "+
            " AND a.user_id = ${userId}  " +
            " limit #{page},#{size}")
    List<GoodsCollectVo> getServeCollectList(@Param("userId") Long userId,
                                             @Param("page") Integer page,
                                             @Param("size") Integer size);

    @Select("SELECT " +
            " a.attention_type_id AS goods_id, " +
            " a.id, " +
            " a.merchant_id, " +
            " b.`name` AS goods_name, " +
            " b.price, " +
            " b.img  " +
            "FROM " +
            " tb_attention_manage AS a " +
            " LEFT JOIN tb_device AS b ON a.attention_type_id = b.id  " +
            " WHERE " +
            " a.type = 0 "+
            " AND a.user_id = ${userId} "+
            " limit #{page},#{size}" )
    List<GoodsCollectVo> getDeviceCollectList(@Param("userId") Long userId,
                                             @Param("page") Integer page,
                                             @Param("size") Integer size);

    @Select("<script>" +
            "SELECT " +
            " count( id ) AS `count`  " +
            "FROM " +
            " (( " +
            "  SELECT " +
            "   a.id  " +
            "  FROM " +
            "   tb_attention_manage AS a " +
            "   LEFT JOIN tb_device AS b ON a.attention_type_id = b.id  " +
            "  WHERE " +
            "  a.user_id = ${userId}  " +
            " <if test='goodsName!=null and goodsName!=\"\"'>  AND b.name like '%${goodsName}%'  </if>  " +
            "   ) UNION ALL " +
            "  ( " +
            "  SELECT " +
            "   a.id  " +
            "  FROM " +
            "   tb_attention_manage AS a " +
            "   LEFT JOIN tb_serve AS b ON a.attention_type_id = b.id  " +
            "  WHERE " +
            "  a.user_id = ${userId}  " +
            " <if test='goodsName!=null and goodsName!=\"\"'>  AND b.name like '%${goodsName}%'  </if>  " +
            " )) AS T" +
            "</script>")
    Long getShopsCollectCount(@Param("goodsName") String goodsName,
                              @Param("userId") Long userId);


    @Select("select  count(*) from tb_attention_manage where merchant_id='${userId}' and DATE(create_time) like '${time}'")
    Double listCollectByTime(@Param("userId") Long userId,@Param("time") String time);

    /**
     * 根据时间查询收藏人数
     * @param userId
     * @param time
     * @return
     */
    @Select("select  count(DISTINCT user_id) from tb_attention_manage where merchant_id='${userId}' and DATE(create_time) like '${time}'")
    Double listCollectPersonByTime(@Param("userId") Long userId,@Param("time") String time);

    /**
     * 总商品收藏
     * @param userId
     * @return
     */
    @Select("select  count(*) from tb_attention_manage where merchant_id='${userId}'")
    Double countCollect(Long userId);

    /**
     * 总收藏人数
     * @param userId
     * @return
     */
    @Select("select  count(DISTINCT user_id) from tb_attention_manage where merchant_id='${userId}'")
    Double countCollectPerson(Long userId);
}
